package tw.com.hitevision.whiteboard.android.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import tw.com.hitevision.whiteboard.android.Props;
import tw.com.hitevision.whiteboard.android.R;
import tw.com.hitevision.whiteboard.android.Settings;

public class TutorialMenu extends ConstraintLayout {
    private static final String TAG =TutorialMenu.class.getSimpleName();
    private TutorialPagerAdapter pagerAdapter;
    private TextView title;
    private TextView description;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ArrayList<View> pages;
    private View close;
    private TextView back;
    private TextView next;
    private TextView finish;

    public TutorialMenu(Context context) {
        super(context);
        initView(context);
    }

    public TutorialMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TutorialMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);


    }

    private void initView(Context context){
        Settings settings = Settings.getInstance(context);

        inflate(context, R.layout.tutorial_layout ,this);
        title = findViewById(R.id.title);
        description = findViewById(R.id.description);
        description.setText(R.string.update_tour_plam_eraser);
        finish = findViewById(R.id.tutorial_finish);
        next = findViewById(R.id.next);
        back = findViewById(R.id.back);
        close = findViewById(R.id.close);
        pages = new ArrayList<>();
        int viewCount = 0;
        if(Props.isFake4k()){
            pages.add(new View(context));
            pages.get(viewCount++).setBackgroundResource(R.drawable.img_whatsnew_eraser_4k);
        }else{
            pages.add(new View(context));
            pages.get(viewCount++).setBackgroundResource(R.drawable.img_whatsnew_eraser);
        }

        pagerAdapter = new TutorialPagerAdapter(pages);
        viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(new GuidePageChangeListener());
        tabLayout = findViewById(R.id.tabs);
        tabLayout.setVisibility(GONE);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(0);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        tabLayout.setupWithViewPager(viewPager);

        finish.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setVisibility(GONE);
                settings.setFlagNeedTutorial(false);
            }
        });
        next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
            }
        });
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
            }
        });
        close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setVisibility(GONE);
                settings.setFlagNeedTutorial(false);
            }
        });

        setVisibility(VISIBLE);
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });


    }


    static class TutorialPagerAdapter extends PagerAdapter {
        private final ArrayList<View> pageview1;
        public TutorialPagerAdapter(ArrayList<View> pageview1){
            this.pageview1 = pageview1;
        }
        @Override
        public void destroyItem(@NotNull ViewGroup container, int position, @NotNull Object object) {
            if (pageview1.get(position)!=null) {
                container.removeView(pageview1.get(position));
            }
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(pageview1.get(position));
            return pageview1.get(position);
        }

        @Override
        public int getCount() {
            return pageview1.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return object==view;
        }
    }

    class GuidePageChangeListener implements ViewPager.OnPageChangeListener{
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }


        @Override
        public void onPageSelected(int position) {
            switch (position){
                case 0:
                    title.setText(R.string.update_tour_title);
                    description.setText(R.string.update_tour_plam_eraser);
                    back.setVisibility(GONE);
                    next.setVisibility(GONE);
                    finish.setVisibility(VISIBLE);
                    break;
            }
        }
    }



}
